const redis = require('redis');
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const readFile = promisify(fs.readFile);
const readDir = promisify(fs.readdir);
const writeFile = promisify(fs.writeFile);
const fsStat = promisify(fs.stat);

const gDir=process.cwd()+'\\policy_json\\';//文件存放路径

async function ChangeJsonFile(dir,fileName)
{
  console.log(dir+fileName);
  let jsonStr = await readFile(dir+fileName);
  console.log(jsonStr);
  let jsonData = JSON.parse(jsonStr);

  jsonData.forEach(opt=>{
    delete opt.id;
    delete opt.category;
    delete opt.sub;
    delete opt.lock;
  })

  let newStr = JSON.stringify(jsonData,null,'\t');
  let err = await writeFile(dir+'new\\'+fileName, newStr, 'utf8');
  //console.log(err, newStr);
}

async function GetAllJsonFiles()
{
  let files = await readDir(gDir);

  for(fileName of files){
    let stat = await fsStat(gDir+fileName);

    if(stat.isFile()){
      //console.log(fileName);
      ChangeJsonFile(gDir,fileName);
    }
  }
}

GetAllJsonFiles();